import logging
import time

from django.db import connection

from app.service import order, operation_log
from app.service.app_order import save_app_order
from libs.common import success
from libs.time_helper import ts_to_time_str

logger = logging.getLogger(__name__)


def thread_test():
    print(time.time())


def test(request):
    # try:
    #     wxPay = WeChatPay(
    #         appid=wx_config.get('AppID'),
    #         api_key=wx_config.get('API_KEY'),
    #         mch_id=wx_config.get('MCH_ID'),
    #         mch_cert='D:/workspace/com/py_cashier/app/pem/wx/apiclient_cert.pem',
    #         mch_key='D:/workspace/com/py_cashier/app/pem/wx/apiclient_key.pem',
    #         # sandbox=True
    #     )
    #     goods_desc = 'test'
    #     total_fee = 15
    #     auth_code = '136120468491548188'
    #     res = wxPay.barcodePay.create(goods_desc, total_fee, auth_code)
    #     print(res)
    # except Exception as e:
    #     print(e)
    #     error()

    # payRes, err = WxPay().barcodePay('136116291422735170', 10, 'test', 'wx_order_id')
    # if err is not None:
    #     print(err)
    # else:
    #     print('#######', payRes)
    #     wx_order_id = payRes.get('transaction_id', '')
    #     print('wx_order_id', wx_order_id)

    # payRes, err = Ali().barcodePay('281778618060744546', 'test', 'ali_order_id2', 1)
    # print('#######', payRes)
    # print('#######', err)

    # logger.error('会员信息不存在，无法退还余额。logid=%s', json.dumps(['a', 'b']))
    # trade_no, tp_resp_str, err = WxPay().refund(1, 1, '2201061640091711451E86WC', '退1')
    # print(trade_no)
    # print(tp_resp_str)
    # print(err)
    # trade_no, tp_resp_str, err = Ali().refund(1, '21121311411182436302MZTX', '退0.01')
    # print(trade_no)
    # print(tp_resp_str)
    # print(err)

    # a, b, c, d = WxPay().queryOrder(out_trade_no='211110102830525164JTF5X')
    # print('traded_no', a)
    # print('trade_state', b)
    # print('res', c)
    # print('err', d)

    # order.sync_third_party_payment_order('211024011413394665Z423E', 1, None)
    # print(Ali().queryOrder('2112111406031860700JXUVM'))
    # print(Ali().queryOrder('2112111405149460630E2ALI'))
    # print(Ali().queryOrder('2112131439339276190OMTUG'))
    # print(Ali().queryOrder('2112131533385171760WPQ94'))
    # with connection.cursor() as cursor:
    #     err = order.recover_stock_from_order(cursor, 307)
    #     print(err)
    # counter = 0
    # while counter < 10:
    #     counter += 1
    #     thr = Thread(target=thread_test)
    #     thr.start()

    # err = app_api.app_top_up(6, '21111914004202872609', 5, '15810586617', 1, 1)
    # err = app_api.app_top_up(6, '21111914090802167480', 5, '15810586617', 2, 1)
    # print(err)

    # print(app_api.app_deduct_coupon('22011916211280393408SLGZ', 10))
    return success(ver='0302')


def debug(request):
    operation_log.save({
        'typ_id': 1,
        'title': 'title',
        'operator_id': 1,
    }, request, None)
    datetime_str = ts_to_time_str(time_format="%Y-%m-%d %H:%M:%S")
    app_order_data = {
        'order_id': 'abc',
        'member_id': '123',
        'shoper_name': 'shop_name',
        'order_time': datetime_str,
        'pay_time': datetime_str,
        'total': 3.21,
        'offline_order_id': 'abc',
    }
    app_order_id, err = save_app_order(None, app_order_data)
    print(app_order_id, err)
    return success()
